package com.etrans.baf.analyse.task.swap.controller;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.etrans.baf.analyse.task.swap.service.VehicleSwapService;
import com.etrans.baf.analyse.util.CommonUtil;
import com.etrans.baf.analyse.util.Constants;
import com.etrans.baf.analyse.util.DateUtils;

@Component("vehicleSwapAnalyse")
@Controller
@RequestMapping("/task/swap/vehicleswap/*")
public class VehicleSwapAnalyse{

	@Autowired
	private VehicleSwapService vehicleSwapService;
	
	static final Logger logger = LoggerFactory.getLogger(VehicleSwapAnalyse.class);

	/**
	 * 重分析 生成vehicleSwap信息
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "reVehicle.html")
	public void reVehicle(HttpServletRequest request,HttpServletResponse response){
		Map<String,String> map = new HashMap<String,String>();
		try {
			String dateStr = (request.getParameter("date")!=null)?String.valueOf(request.getParameter("date")):null;
			analyse((dateStr!=null&&dateStr.trim().length()>0?dateStr:null));
			map.put("status", "ok");
		} catch (Exception e) {
			e.printStackTrace();
			map.put("status", "error:"+e.getMessage());
		}
		CommonUtil.ajaxResultJson(response, map);
	}
	/**
	 * 重分析 生成问题车辆信息
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "reProblem.html")
	public void reProblem(HttpServletRequest request,HttpServletResponse response){
		Map<String,String> map = new HashMap<String,String>();
		try {
			vehicleProblemTimer();
			map.put("status", "ok");
		} catch (Exception e) {
			e.printStackTrace();
			map.put("status", "error:"+e.getMessage());
		}
		CommonUtil.ajaxResultJson(response, map);
	}	
	public void vehicleTimer(){
		logger.info("开始分析----------------");
		analyse(null);
		logger.info("分析结束");
	}
	public void vehicleProblemTimer(){
		try {
			 vehicleSwapService.problemAnalyse();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	private  void analyse(String date){
		try {
			String analyse_date = DateUtils.getAnalyseDate(date,0);
			vehicleSwapService.insertVehicleSwrap(analyse_date);
		} catch (Exception e) {
			e.printStackTrace();
		}
	} 
	/**
	 * 中间表周分析
	 */
	public void vehicleWeekTimer(){
		try {
			String start_date = DateUtils.getLastWeekOne(null);
			String end_date = DateUtils.getLastWeekSeven(start_date);
			vehicleSwapService.weekMonthAnalyse(start_date, end_date, Constants.VEHICLE_ANALYSE_W);
		} catch (Exception e) {
			 e.printStackTrace();
			 logger.error("车辆中间表周分析异常:"+e.getMessage());
		}
	}
	/**
	 * 中间表月分析  上月分析
	 */
	public void vehicleMonthTimer(){
		 try {
				String last_mon = DateUtils.getAnalyseDate(null, 1).substring(0,7);
				String current_mon = DateUtils.format(new Date(),Constants.DATE_YYYY_MM);
				vehicleSwapService.weekMonthAnalyse(last_mon+"-25", current_mon+"-24", Constants.VEHICLE_ANALYSE_M);
		} catch (Exception e) {
			 e.printStackTrace();
			 logger.error("车辆中间表月分析异常:"+e.getMessage());
		}
	}
	
	/**
	 * 中间表月分析  当前月实时分析
	 */
	@RequestMapping(value = "vehicleCurMonthTimer.html")
	public void vehicleCurMonthTimer(){
		 try {
				Date cdate = new Date();
				String current_mon_day = DateUtils.format(cdate,Constants.DATE_YYYY_MM_DD);
				String day = current_mon_day.substring(current_mon_day.length()-2,current_mon_day.length());
				String current_mon = DateUtils.format(cdate,Constants.DATE_YYYY_MM);
				if(Integer.parseInt(day)<=25){
					String last_mon = DateUtils.getAnalyseDate(null, 1).substring(0,7);
					vehicleSwapService.weekMonthAnalyse(last_mon+"-25", current_mon+"-24", Constants.VEHICLE_ANALYSE_CM);
				}else{
					String next_month = DateUtils.nextMonthFirstDate().substring(0,7);
					vehicleSwapService.weekMonthAnalyse(current_mon+"-25", next_month+"-24", Constants.VEHICLE_ANALYSE_CM);
				}
		} catch (Exception e) {
			 e.printStackTrace();
			 logger.error("车辆中间表月分析异常:"+e.getMessage());
		}
	}
	/**
	 * 重分析 周分析
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "reVehicleWeek.html")
	public void reVehicleWeek(HttpServletRequest request,HttpServletResponse response){
		Map<String,String> map = new HashMap<String,String>();
		try {
			logger.info("车辆中间维度表周分析  重分析开始-------某个星期一的日期:date:2017-09-25 ;分析日期date="+request.getParameter("date"));
			String start_date = (request.getParameter("date")!=null)?String.valueOf(request.getParameter("date")):DateUtils.getLastWeekOne(null);
 			String end_date = DateUtils.getLastWeekSeven(start_date);
 			vehicleSwapService.weekMonthAnalyse(start_date, end_date, Constants.VEHICLE_ANALYSE_W);
			map.put("status", "ok");
		} catch (Exception e) {
			e.printStackTrace();
			map.put("status", "error:"+e.getMessage());
		}
		CommonUtil.ajaxResultJson(response, map);
	}
	
	/**
	 * 重分析 月分析  上月分析
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "reVehicleMonth.html")
	public void reVehicleMonth(HttpServletRequest request,HttpServletResponse response){
		Map<String,String> map = new HashMap<String,String>();
		try {
			logger.info("车辆中间维度 月分析重开始分析------last_mon:如2017-08,current_mon:如2017-09： last_mon="+request.getParameter("last_mon")+";current_mon="+request.getParameter("current_mon"));
			String last_mon =  (request.getParameter("last_mon")!=null && !"".equals(String.valueOf(request.getParameter("last_mon"))))?String.valueOf(request.getParameter("last_mon")):DateUtils.getAnalyseDate(null, 1).substring(0,7);
			String current_mon = (request.getParameter("current_mon")!=null&& !"".equals(String.valueOf(request.getParameter("current_mon"))))?String.valueOf(request.getParameter("current_mon")):DateUtils.format(new Date(),Constants.DATE_YYYY_MM);
 			vehicleSwapService.weekMonthAnalyse(last_mon+"-25", current_mon+"-24", Constants.VEHICLE_ANALYSE_M);
			map.put("status", "ok");
		} catch (Exception e) {
			e.printStackTrace();
			map.put("status", "error:"+e.getMessage());
		}
		CommonUtil.ajaxResultJson(response, map);
	}
}
